<?php
/**
 * PageBar.php	
 *
 * @package		
 * @Copyright	(c) All rights reserved
 * @Author		skyCrack <skyCrack@126.com>
 * @Version		$Id$
 */

class PageBar {
	public $total			=	0;		//total run num
	public $offset			=	0;
	public $num				=	0;		//row num
	
	public $page			=	0;		//current page
	public $totalPages		=	0;		//total pages
	
	private $_linkHead		=	'';
	private $_linkTpl		=	'';
	
	private $_options		=	array(
		'prePageTag'	=>	'[<]',
		'nextPageTag'	=>	'[>]',
		'preGroupTag'	=>	'[<<]',
		'nextGroupTag'	=>	'[>>]',
		'leftTag'		=>	'[',
		'rightTag'		=>	']',
		'class'			=>	''
	);
	
	public function __construct($total, $num) {
		$this->total	=	$total;
		$this->num		=	$num;
		$this->totalPages	=	ceil( $total / $num );
		
		$page	=	( isset($_GET['page']) ) ? (int)$_GET['page'] : 0;
		
		if ( $page > $this->totalPages )
			$page	=	$this->totalPages;
		
		if ( $page > 1 ) {
			$this->page		=	$page;
			$this->offset		=	( $page - 1 ) * $num;
		} else {
			$this->page		=	1;
			$this->offset		=	0;
		}
		
		//initialize
		
		$temp				= explode('?',$_SERVER['REQUEST_URI']);  //lg
		$this->_linkHead	= $temp[0] . '?';
		if ( !empty($_SERVER['QUERY_STRING']) ) {
			$data = array();
			parse_str($_SERVER['QUERY_STRING'], $data);
			unset($data['page']);
			
			if ( count($data) > 0 )
				$this->_linkHead .=  http_build_query($data, 'var_') . '&';
			
		}
	
		$this->_linkTpl	=	"<a href='$this->_linkHead" . "page={page}' class='" . $this->_options['class'] . "'>{view}</a>";
	}
	
	public function setOptions($options) {
		foreach ( $options as $name => $value ) {
			$this->_setOption($name, $value);
		}
		
		$this->_linkTpl	=	"<a href='$this->_linkHead" . "page=%d' class='" . $this->_options['class'] . "'>%s</a>";
	}
	
	private function _setOption($name, $value) {
		if ( array_key_exists($name, $this->_options) ) {
			$this->_options[$name]	=	$value;
		}
	}
	
	/**
	 * set link template
	 */
	public function setLinkTpl($linkTpl) {
		$this->_linkTpl	=	$linkTpl;
	}
	
	private function _link($page, $view) {
		return preg_replace(array('/{page}/', '/{view}/'), compact('page', 'view'), $this->_linkTpl);
	}
	
	public function prePage($view = '') {
		if ( empty($view) )
			$view	=	$this->_options['prePageTag'];
		
		if ( $this->page > 1 ) {
			return $this->_link($this->page -1, $view);
		} else {
			return '';
		}
	}
	
	public function nextPage($view = '') {
		if ( empty($view) )
			$view	=	$this->_options['nextPageTag'];
		
		if ( $this->page < $this->totalPages ) {
			return $this->_link($this->page + 1, $view);
		} else {
			return '';
		}
	}
	
	public function preGroup($pageNum, $view = '') {
		if ( empty($view) )
			$view = $this->_options['preGroupTag'];
		
		$preGroupPage = ( $this->page > $pageNum ) ? $this->page - $pageNum : 1;
		
		return $this->_link($preGroupPage, $view);
	}
	
	public function nextGroup($pageNum, $view = '') {
		
		if ( empty($view) )
			$view = $this->_options['nextGroupTag'];
		
		$mid       = floor( $this->num / 2);
		$minPage   = ( $this->page < $mid ) ? 1 : ($this->page - $mid);
		$maxPage   = $minPage + $pageNum;
		
		if ( $maxPage > $this->totalPages )
			$maxPage = $this->totalPages;

		$nextGroupPage = ( $this->totalPages > ($maxPage + $mid) ) ? $maxPage + $mid : $this->totalPages;
		
		return $this->_link($nextGroupPage, $view);
	}
	
	public function numBar($pageNum = 13, $color='#FF0000', $left='', $right='') {
		if ( empty($left) )
			$left	=	$this->_options['leftTag'];
			
		if ( empty($right) )
			$right	=	$this->_options['rightTag'];

		$mid		=	floor( $pageNum / 2);
		$minPage	=	( $this->page > $mid ) ? ($this->page - $mid) : 1;
		$maxPage	=	$minPage + $pageNum - 1;
		
		if ( $maxPage > $this->totalPages ) {
			$maxPage	=	$this->totalPages;
			$minPage	=	( $maxPage > ($pageNum - 1) ) ? $maxPage - $pageNum + 1: 1;
		}
		
		$linkBar	=	'';
		
		for ($i = $minPage; $i <= $maxPage; $i++) {
			$view = $left . $i . $right;
			
			if ( $i == $this->page )
				$linkChar = "<font color='$color'>$view</font>";
			else 
				$linkChar = $this->_link($i, $view);
				
			$linkBar  = $linkBar . $linkChar;
		}
		
		return $linkBar;
	}
	
	public function bar($num = 13, $color = '#FF0000') {
		$numBar    = $this->numBar($num, $color);
		$preGroup  = $this->preGroup($num);
		$prePage   = $this->prePage();
		$nextPage  = $this->nextPage();
		$nextGroup = $this->nextGroup($num);
		
		return  $preGroup . $prePage . $numBar . $nextPage . $nextGroup." 共{$this->totalPages}页,{$this->total}条记录";
	}
	
	public function JumpForm() {
		$form = <<<EOT
	<script language="javascript">
	<!--
		function jump(linkHead, total, page)
		{
			var page = ( page.value > total ) ? total : page.value;
			
			page = ( page < 1 ) ? 1 : page;

			location.href = linkHead + "page=" + page;
				
		}
	-->
	</script>
		<form name="pageBarJumpForm" method="GET" onSubmit="return jump('$this->_linkHead', $this->total, this.form.page)">
			<input name="page" type="text" size="1"><input type="button" name="Submit" value="GO" onClick="return jump('$linkHead', $total, this.form.page)">&nbsp;页
		</form>
EOT;

	return $form;
	}
	public function getOrderLink() {
		
		$temp				= explode('?',$_SERVER['REQUEST_URI']);  //lg
		$OrderLink	= $temp[0] . '?';
		if ( !empty($_SERVER['QUERY_STRING']) ) {
			$data = array();
			parse_str($_SERVER['QUERY_STRING'], $data);
			unset($data['page']);
			unset($data['orderBy']);
			$data['asc']	= $data['asc']=='asc'?'desc':'asc';
			if ( count($data) > 0 )
				$OrderLink .=  http_build_query($data, 'var_') . '&';
			
		}
		return $OrderLink ;
	}
}
?>